package utils;

import core.AEntityContentProvider;

public class Picker {

	// method for single object
	public static String[][] picker(AEntityContentProvider object)
			throws Exception {
		Object[] singleObject = { object };
		return picker(singleObject);
	}

	public static String[][] picker(Object[] objects) throws Exception{
                AEntityContentProvider contentProvider;
		// calculate the length of the array
		int lengthArray = 0;

		for (int i = 0; i < objects.length; i++) {
                     contentProvider = 	(AEntityContentProvider) objects[i];
                     contentProvider.reflect();
		      lengthArray += contentProvider.getEntityContent().size();
		}

		String[][] answer = new String[lengthArray][4];
		int rows = 0, countRowsForObject = 0;

		for (int i = 0; i < objects.length; i++) {
			rows += countRowsForObject;
			countRowsForObject = 0;
                        contentProvider = (AEntityContentProvider) objects[i];
                        
			for (int j = 0; j < contentProvider.getEntityContent().size(); j++) {
				for (int k = 0; k < 1; k++) {

					Object id = contentProvider.getIdEntity();
					if (id != null) {
						answer[rows + countRowsForObject][0] = id.toString();
					}

					answer[rows + countRowsForObject][1] = contentProvider.getNameTable();

					answer[rows + countRowsForObject][2] = contentProvider.getAllAttributesNames()[j].toString();
					;

					Object value = contentProvider.getEntityContent().get(
							contentProvider.getAllAttributesNames()[j]);
					if (value != null) {
						answer[rows + countRowsForObject][3] = value.toString();
					}

					countRowsForObject++;
				}
			}
		}
		return answer;
	}

	// method for single object ServiceResponse
	public static String[][] picker(ServiceResponse object) {
		ServiceResponse[] singleObject = { object };
		return picker(singleObject);
	}

	// method for single object ServiceResponse
	public static String[][] picker(ServiceResponse[] object) {
		// calculate the length of the array
		int lengthArray = 0;
		for (int i = 0; i < object.length; i++) {
			for (int j = 0; j < object[i].getAllValuesForAttributes().size(); j++)
				lengthArray += object[i].getAllValuesForAttributes().get(
						object[i].getAllAttributes()[j]).length;
		}

		String[][] answer = new String[lengthArray][4];
		int rows = 0, countRowsForObject = 0;

		for (int i = 0; i < object.length; i++) {
			rows += countRowsForObject;
			countRowsForObject = 0;
			for (int j = 0; j < object[i].getAllValuesForAttributes().size(); j++) {
				for (int k = 0; k < object[i].getAllValuesForAttributes().get(
						object[i].getAllAttributes()[j]).length; k++) {
					answer[rows + countRowsForObject][0] = object[i].getID();
					answer[rows + countRowsForObject][1] = object[i].getType();
					answer[rows + countRowsForObject][2] = object[i]
							.getAllAttributes()[j];
					answer[rows + countRowsForObject][3] = object[i]
							.getAllValuesForAttributes().get(
									object[i].getAllAttributes()[j])[k];
					countRowsForObject++;
				}
			}
		}
		return answer;
	}
}
